package com.taskschecker;

import java.util.Calendar;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.LinearLayout.LayoutParams;

@SuppressLint("ViewConstructor")
public class CalendarDayCell extends View {

	private static final int CELL_TEXT_SIZE = 20;
	public static int ANIM_ALPHA_DURATION = 100;

	private OnItemClick itemClick = null;
	private Paint pt = new Paint();
	private RectF rect = new RectF();
	private RectF rectP = new RectF();
	private String sDate = "";
	private String percent;

	private int currentYear = 0;
	private int currentMonth = 0;
	private int currentDay = 0;

	private boolean isSelected = false;
	private boolean isActiveMonth = false;
	private boolean isToday = false;
	private boolean isTouchedDown = false;
	private boolean isHoliday = false;

	public CalendarDayCell(Context context, int width, int height) {
		super(context);
		setFocusable(true);
		setLayoutParams(new LayoutParams(width, height));
	}

	public interface OnItemClick {
		public void OnClick(CalendarDayCell item);
	}

	public Calendar getDate() {
		Calendar calDate = Calendar.getInstance();
		calDate.clear();
		calDate.set(Calendar.YEAR, currentYear);
		calDate.set(Calendar.MONTH, currentMonth);
		calDate.set(Calendar.DAY_OF_MONTH, currentDay);
		return calDate;
	}

	public void setData(int year, int month, int day, Boolean isToday,
			Boolean isHoliday, int activeMonth, String dayPercent) {
		currentYear = year;
		currentMonth = month;
		currentDay = day;

		this.sDate = Integer.toString(currentDay);
		this.isActiveMonth = (currentMonth == activeMonth);
		this.isToday = isToday;
		this.isHoliday = isHoliday;
		this.percent = dayPercent;
	}

	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		rect.set(0, 0, this.getWidth(), this.getHeight() / 2 + 2);
		rect.inset(1, 1);

		rectP.set(0, this.getHeight() / 2, this.getWidth(), this.getHeight());
		rectP.inset(1, 0);

		final boolean isFocused = isViewFocused();
		drawDayView(canvas, isFocused);
		if (isActiveMonth) {
			drawDayNumber(canvas);
			drawDayPercent(canvas);
		}
	}

	public boolean isViewFocused() {
		return (this.isFocused() || isTouchedDown);
	}

	private void drawDayView(Canvas canvas, boolean isFocused) {

		if (isSelected || isFocused) {
			LinearGradient lGradBkg = null;

			if (isFocused) {
				lGradBkg = new LinearGradient(rect.left, 0, rect.right, 0,
						0xffaa5500, 0xffffddbb, Shader.TileMode.CLAMP);
			}

			if (isSelected) {
				lGradBkg = new LinearGradient(rect.left, 0, rect.right, 0,
						0xff225599, 0xffbbddff, Shader.TileMode.CLAMP);
			}

			if (lGradBkg != null) {
				pt.setShader(lGradBkg);
				canvas.drawRect(rect, pt);
				canvas.drawRect(rectP, pt);
			}

			pt.setShader(null);

		} else {
			pt.setColor(TasksCheckerActivity.day_BgColor);
			canvas.drawRect(rect, pt);

//			if (percent != null && !percent.equals("")) {
//				int perInt = Integer.valueOf(percent.substring(0,
//						percent.length() - 1));
//				if (perInt == 0) {
//					pt.setColor(TasksCheckerActivity.percent_BgColor_0);
//				} else if (perInt < 100) {
//					pt.setColor(TasksCheckerActivity.percent_BgColor_50);
//				}
//			}
			canvas.drawRect(rectP, pt);
		}
	}

	public void drawDayPercent(Canvas canvas) {
		pt.setTypeface(null);
		pt.setAntiAlias(true);
		pt.setShader(null);
		pt.setFakeBoldText(false);
		pt.setTextSize(CELL_TEXT_SIZE);
		pt.setColor(TasksCheckerActivity.isPresentMonth_FontColor);
		pt.setUnderlineText(false);

		final int iPosX = (int) rectP.left + ((int) rectP.width() >> 1)
				- ((int) pt.measureText(percent) >> 1);

		final int iPosY = (int) (this.getHeight()
				- (this.getHeight() / 2 - getTextHeight()) / 4 - pt
				.getFontMetrics().bottom);

		canvas.drawText(percent, iPosX, iPosY, pt);

		pt.setUnderlineText(false);
	}

	public void drawDayNumber(Canvas canvas) {
		pt.setTypeface(null);
		pt.setAntiAlias(true);
		pt.setShader(null);
		pt.setFakeBoldText(true);
		pt.setTextSize(CELL_TEXT_SIZE);
		if (isHoliday)
			pt.setColor(TasksCheckerActivity.calendarHolidayFontColor);
		else
			pt.setColor(TasksCheckerActivity.isPresentMonth_FontColor);
		pt.setUnderlineText(false);

		if (isToday)
			pt.setUnderlineText(true);

		final int iPosX = (int) rect.left + ((int) rect.width() >> 1)
				- ((int) pt.measureText(sDate) >> 1);

		final int iPosY = (int) (this.getHeight() / 2
				- (this.getHeight() / 2 - getTextHeight()) / 4 - pt
				.getFontMetrics().bottom);

		canvas.drawText(sDate, iPosX, iPosY, pt);

		pt.setUnderlineText(false);
	}

	private int getTextHeight() {
		return (int) (-pt.ascent() + pt.descent());
	}

	public void setSelected(boolean isEnable) {
		if (this.isSelected != isEnable) {
			this.isSelected = isEnable;
			this.invalidate();
		}
	}

	public void setItemClick(OnItemClick itemClick) {
		this.itemClick = itemClick;
	}

	public void doItemClick() {
		if (itemClick != null)
			itemClick.OnClick(this);
	}

	public boolean onTouchEvent(MotionEvent event) {
		boolean isHandled = false;
		if (!isActiveMonth) {
			return isHandled;
		}
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			isHandled = true;
			isTouchedDown = true;
			invalidate();
			startAlphaAnimIn(CalendarDayCell.this);
		}
		if (event.getAction() == MotionEvent.ACTION_CANCEL) {
			isHandled = true;
			isTouchedDown = false;
			invalidate();
		}
		if (event.getAction() == MotionEvent.ACTION_UP) {
			isHandled = true;
			isTouchedDown = false;
			invalidate();
			doItemClick();
		}
		return isHandled;
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean isResult = super.onKeyDown(keyCode, event);
		if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
				|| (keyCode == KeyEvent.KEYCODE_ENTER)) {
			doItemClick();
		}
		return isResult;
	}

	public static void startAlphaAnimIn(View view) {
		AlphaAnimation anim = new AlphaAnimation(0.5F, 1);
		anim.setDuration(ANIM_ALPHA_DURATION);
		anim.startNow();
		view.startAnimation(anim);
	}

}
